<%@ page  pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="org.myhibernate.util.Json"%>
<%@page import="org.myhibernate.core.method.PageSizeQuery"%>
<%@page import="org.myhibernate.Template"%>
<%@page import="org.myhibernate.ProxyTemplate"%>
<%@page import="org.json.JSONArray"%>
<%@page import="org.json.JSONObject"%>
<%
request.setCharacterEncoding("utf-8");
Class cls=Object.class;
	if (request.getParameter("classname")!=null) {
			try {
				cls=Class.forName(request.getParameter("classname")).newInstance().getClass();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
int start=0;
int size=10;
if(request.getParameter("start")!=null){start=Integer.parseInt(request.getParameter("start")+"");}
if(request.getParameter("size")!=null){size=Integer.parseInt(request.getParameter("size")+"");}

StringBuffer extSql=new StringBuffer();
String sortSql=request.getParameter("sortsql");
if(sortSql==null){sortSql="";}
try{
	sortSql=URLDecoder.decode(sortSql,"utf-8");
}catch(Exception exception){}
int count=0;
if(request.getParameter("f0")!=null)
{
	extSql.append(" where ");
}
while(request.getParameter("f"+count)!=null)
{
	String field=request.getParameter("f"+count);
	String value=request.getParameter("v"+count)+"";
	try{
		value=URLDecoder.decode(value,"utf-8");
	}catch(Exception exception){}
	String condition="and";
	if(request.getParameter("c"+count)!=null)
	{
		condition=request.getParameter("c"+count);
	}
	if(0 == count)
	{
		extSql.append(" ("+field+" like '%"+value+"%') ");
	}else
	{
		extSql.append(" "+condition+" ("+field+" like '%"+value+"%') ");
	}
	count++;
}
extSql.append(" "+sortSql);
System.out.println("oag.action.query.jsp sql="+extSql.toString());
Template template=new ProxyTemplate(cls).getInstance();
Json json=new Json();
try{ 
	List list=null;
	PageSizeQuery query=(PageSizeQuery)template.getPageSizeQuery();
	
	int allCount=query.query(extSql.toString());
	list=query.getListObject(start,start+size);
	
	JSONArray dataList=(JSONArray) JSONObject.wrap(list);
	json.add("result","ok");
	json.add("start",start);
	json.add("size",size);
	json.add("count",allCount);
	json.add("hasid",true);
	json.add("data",dataList);
	//json.add("sql",query.getSql());
}catch(Exception exception){
	json.add("result","no");
	json.add("message",exception.getMessage());
}finally{
	if(template!=null)
	{
		template.close();
	}
}
out.print(json);
%>

